#!/usr/bin/env python
# coding: utf-8
import jenkins
from serviceinfo.app_type_config import *
from config import setting
def insert_jenkins(proname,env_type,app_type,group,gitaddr,git_releases):
    # from serviceinfo import models
    gg = ''
    for gitrr in git_releases.split("\n"):
        gg += "<string>%s</string>\n"%gitrr
    app_name = group + "-" + env_type + "_" + proname
    res = {"status":None}
    print("aaaaaa")
    # server = jenkins.Jenkins('http://10.37.251.96:8080', username='ops', password='ops@2018')
    server = jenkins.Jenkins('http://jk.iqdnet.cn', username=setting.JINKINS_USER, password=setting.JINKINS_PASS)
    print(server)
    jobs = server.get_jobs()
    print("jjjj", jobs)
    jobs_list = []
    for job in jobs:
        jobs_list.append(job["name"])
    print("lllll", jobs_list)
    if app_name in jobs_list:
        res["status"] = 500
    else:
        print("zzzz",app_name)
        if app_type == 1:
            tomcat_conf = tomcat_config.replace("qdapptype",env_type).replace("qdingapp",proname).replace("qdinggitaddr",gitaddr).replace("qdinggroup",group)
            print(tomcat_conf)
            try:
                server.create_job(app_name,tomcat_conf)
                res["status"] = 200
            except Exception as e:
                res["status"] = 400
        elif app_type == 2:
            jar_conf = jar_config.replace("qdapptype",env_type).replace("qdingapp",proname).replace("qdinggitaddr",gitaddr).replace("qdinggroup",group)
            print(jar_conf)
            try:
                print("zzzz", app_name)
                server.create_job(app_name,jar_conf)
                res["status"] = 200
            except Exception as e:
                res["status"] = 400
    return res

# insert_jenkins("rrrrr","dev","tomcat","qding","rrrr.git","master")


def jenkins_job_del(proname,env_type,group):
    # print(77777777777777)
    res = {"status": None}
    app_name = group + "-" + env_type + "_" + proname
    server = jenkins.Jenkins('http://jk.iqdnet.cn', username='sungaohui', password='!QAZ2wsx')
    print(server)
    try:
        server.delete_job(app_name)
        res["status"] = 200

    except Exception as e:
        res["status"] = 400
    return res